package net.fdukedom.epicurus.client.ui.dialogs;

import java.awt.Dimension;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import net.fdukedom.epicurus.client.connection.PluginServiceWrapper;
import net.fdukedom.epicurus.client.ui.GUIDispatcher;
import net.fdukedom.epicurus.domain.entity.Plugin;
import net.fdukedom.epicurus.domain.util.PluginHelper;
import net.fdukedom.epicurus.plugin.PluginRegistrar;
import net.fdukedom.epicurus.plugin.XMLPluginLoader;

/**
 * 
 * @author Alexander Kirakozov
 */
public class PluginManagerDlg extends ModalDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Cancel&Ok button
	 */
	private JButton cancelOkButton;
	
	/**
	 * Model for table plugin view
	 * @author Alexander Kirakozov
	 */
	class PluginTableModel extends AbstractTableModel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * Table data
		 */
		private Object[][] data;

		/**
		 * Column names
		 */
		private String[] columnNames = { "Name", "Version" };

		/**
		 * Constructor
		 *
		 */
		public PluginTableModel() {
			loadPluginInfo();
		}

		/**
		 * Returns rows count
		 */
		public int getRowCount() {
			if ( data != null )
			{
				return data.length;
			}
			return 0;
		}

		/**
		 * Returns columns count
		 */
		public int getColumnCount() {
			return columnNames.length;
		}
		
		/**
		 * Returns cell value 
		 */
		public Object getValueAt(int arg0, int arg1) {
			return data[arg0][arg1];
		}

		/**
		 * Return column name
		 */
		public String getColumnName(int col) {
			return columnNames[col];
		}

		/**
		 * Load information about plugins to DB
		 */
		public void loadPluginInfo() {
			PluginHelper helper = PluginHelper.getInstance();
			Map<String, Plugin> plugins = helper.loadPluginsMap();
			
			if (plugins != null && plugins.size() != 0) {
				String[][] info = new String[plugins.size()][2];
				int i = 0;

				for (Plugin plugin : plugins.values()) {
					info[i][0] = plugin.getName();
					info[i][1] = Integer.toString(plugin.getVersion());
					i++;
				}
				data = info;
			}
			
			fireTableDataChanged();
		}
	}

	/**
	 * Table model for showing plugins
	 */
	private PluginTableModel tableModel;

	/**
	 * Constructor
	 * 
	 * @param frame
	 *            main frame
	 */
	public PluginManagerDlg(JFrame frame) {
		super(frame);
		setContentPane(createPanel());
		init("Plugin manager", frame);
	}

	/**
	 * Action listener for saving connection options
	 * 
	 * @author Alexander Kirakozov
	 */
	private class UpdateActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			PluginServiceWrapper plugWrap = new PluginServiceWrapper();
			String xmlPlugins = plugWrap.getPluginsInfo();
			XMLPluginLoader loader = new XMLPluginLoader(xmlPlugins);

			List<Plugin> plugins = loader.loadPlugins();
			PluginRegistrar.register(plugins);
			tableModel.loadPluginInfo();
			cancelOkButton.setText("Ok");
			GUIDispatcher.getInstance().showPage(null, GUIDispatcher.INDEX_PAGE_ID, null);
		}
	}

	/**
	 * Creates dialog panel
	 * @return dialog panel
	 */
	private JPanel createPanel() {
		JPanel box = new JPanel();
		box.setLayout(new FlowLayout());

		tableModel = new PluginTableModel();
		JTable table = new JTable(tableModel);
		table.setEnabled(false);

		JScrollPane scrollPane = new JScrollPane(table);
		table.setPreferredScrollableViewportSize(new Dimension(400, 70));
		box.add(scrollPane);

		// Update & cancel buttons
		JPanel buttons = new JPanel();
		buttons.setLayout(new FlowLayout());

		JButton button = new JButton("Update");
		button.addActionListener(new UpdateActionListener());
		buttons.add(button);

		cancelOkButton = new JButton("Cancel");
		cancelOkButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				close();

			}
		});
		buttons.add(cancelOkButton);

		// Main panel
		JPanel panel = new JPanel();

		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add(box);
		panel.add(buttons);

		return panel;
	}
}
